#include "stdafx.h"
#include "Lex.h"
#include "ast\ScriptAst.h"
#include "ast\StatementAst.h"
#include "Interpreter.h"

using namespace System;

namespace squirrel
{
	namespace interpreter
	{
		void Interpreter::Execute(System::String ^sqls)
		{
			if(String::IsNullOrEmpty(sqls))
			{
				throw gcnew ArgumentNullException("sqls");
			}

			try
			{
				Lex^ lex = gcnew Lex(sqls);
				ScriptAst^ script = gcnew ScriptAst();
				int pos = 0;
				script->Parse(lex->Tokens, pos);

				for each(StatementAst^ statement in script->Statements)
				{
					try
					{
						ShowHint(statement->ToString());
						statement->Execute();
						Console::WriteLine();
					}
					catch(Exception^ ex)
					{
						ShowError(ex->Message);
					}
				}
			}
			catch(Exception^ ex2)
			{
				ShowError(ex2->Message);
			}
		}
	}
}